package com.block.common.security.service.impl.reqData;

import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.symmetric.AES;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.security.service.ReqDataTransferService;
import com.block.common.service.LogFactory;
import com.block.db.entity.AppEncrypt;
import org.slf4j.Logger;

public class MiniReqTransferService extends ReqDataTransferService {

    private static Logger logger= LogFactory.getAppReq();

    @Override
    public JSONObject transferReqData(AppEncrypt appEncrypt, JSONObject jsonObject, String methodName) throws Exception {
        String encTranData=jsonObject.getString("tranData");
        String ciphertext=jsonObject.getString("ciphertext");

        String keyInfo = SecureUtil.rsa(appEncrypt.getPriKey(), null).decryptStr(ciphertext, KeyType.PrivateKey);
        JSONObject keyObject = JSON.parseObject(keyInfo);
        String key = keyObject.getString("key");
        String iv = keyObject.getString("iv");

        String originTranData = new AES(Mode.CBC, Padding.PKCS5Padding, key.getBytes(), iv.getBytes()).decryptStr(encTranData);

        logger.info("解密后的tranData{}",JSON.toJSONString(originTranData));
        return JSONObject.parseObject(originTranData);
    }

}
